package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.task.ArticleHtmlTasks;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-29
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private ArticleHtmlTasks articleHtmlTask;

    @Override
    public ResponseResult<Long> saveApArticle(ArticleDto dto) {
//        判断是否有ID
        if (dto.getId()==null) { //新增
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
//            新增文章信息
            this.save(apArticle);
//            新增文章内容
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
            //异步 线程池
            articleHtmlTask.createHtml(apArticle,dto.getContent());
            return ResponseResult.okResult(apArticle.getId());
        }else{
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
//            修改文章信息
            this.updateById(apArticle);
//           修改文章内容
//            update ap_article_content set content=?? where article_id=??
            LambdaUpdateWrapper<ApArticleContent> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ApArticleContent::getContent,dto.getContent());
            updateWrapper.eq(ApArticleContent::getArticleId,dto.getId());
            apArticleContentService.update(updateWrapper);
            return ResponseResult.okResult(apArticle.getId());
        }

    }


    /**
     * 加载文章列表
     *
     * @param dto
     * @param type 0 最新  1  更多
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, int type) {

        // 判断是否是首页加载数据,如果是首页的话,先从redis中查询数据
        if (dto.getLoaddir() == 1) {
            // 先从redis中查询数据,按照分值从大到小排序
            String key = "hot_article_first_page_" + dto.getChannelId();
            long start = 0;
            long end = dto.getSize() - 1;
            Set<String> strings = redisTemplate.boundZSetOps(key).reverseRange(start, end); // reverseRange根据分值降序查询
            List<ApArticle> articles = new ArrayList<>();
            for (String string : strings) {
                ApArticle article = JSON.parseObject(string, ApArticle.class);
                articles.add(article);
            }
            if (articles.size() >= dto.getSize()) {
                return ResponseResult.okResult(articles);
            }
        }

        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        //        查询发布时间大于dto中的maxTime
        if(type==0){
            queryWrapper.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }else{
            queryWrapper.lt(ApArticle::getPublishTime,dto.getMinTime());
        }
        //        如果dto中的channelId不为0时 根据频道查询
        if(dto.getChannelId()!=0){
            queryWrapper.eq(ApArticle::getChannelId,dto.getChannelId());
        }
//        两个隐藏的条件 1、是否删除 2、是否下架
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);
//        根据发布时间降序查询
        queryWrapper.orderByDesc(ApArticle::getPublishTime);
//        分页查询

        IPage<ApArticle> page = new Page<>(1,dto.getSize());
        IPage<ApArticle> page1 = this.page(page, queryWrapper);
        return ResponseResult.okResult(page1.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle == null || apArticle.getIsDelete() || apArticle.getIsDown()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 根据id查询文章内容
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(query);
        // 按照前端的格式拼接map
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        // 返回通用的响应
        return ResponseResult.okResult(map);
    }


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void compute() {
        //        获取今天时间和 5天前的时间
//        LambdaQueryWrapper<ApArticle> queryWrapper = Wrappers.lambdaQuery();
        //        使用了joda-time中的工具类 ，因为最近测试数据少，这里可以查询200天之前的数据
        Date beforeDate = DateTime.now().minusDays(200).toDate();
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);
        queryWrapper.ge(ApArticle::getPublishTime,beforeDate);
        List<ApArticle> list = this.list(queryWrapper);


        //        先删除再放数据
        Set<String> keys = redisTemplate.keys("hot_article_first_page_*");

        redisTemplate.delete(keys);



        List<ApArticle> articleList = this.list(queryWrapper);
        for (ApArticle article : articleList) {
            // 计算文章的分值
            int score = computeScore(article);

            ApArticle cache = new ApArticle();

            cache.setId(article.getId());
            cache.setTitle(article.getTitle());
            cache.setImages(article.getImages());
            cache.setAuthorId(article.getAuthorId());
            cache.setAuthorName(article.getAuthorName());
            cache.setLayout(article.getLayout());
            cache.setStaticUrl(article.getStaticUrl());

            String jsonString = JSON.toJSONString(cache); //放的内容是固定不变的，比如点赞数、评论数经常变的属性就不要放了，如果理解不了就直接把article转成json字符串
            //            0代表是首页上推荐的文章
            redisTemplate.boundZSetOps("hot_article_first_page_0").add(jsonString,score);
            //            再放到各自的频道上推荐的文章
            redisTemplate.boundZSetOps("hot_article_first_page_"+article.getChannelId()).add(jsonString,score);

        }

    }

    private int computeScore(ApArticle article) {
        int score = 0;
        // 阅读 +1  点赞 +3  评论 +5  收藏 +8
        if (article.getViews() != null) {
            score += article.getViews() * 1;
        }
        if (article.getLikes() != null) {
            score += article.getLikes() * 3;
        }
        if (article.getComment() != null) {
            score += article.getComment() * 5;
        }
        if (article.getCollection() != null) {
            score += article.getCollection() * 8;
        }
        return score;
    }
}
